import { getData } from '@vimi/utils-api'
import { addressCut } from '@vimi/utils-tool'
const state = {
  accessOrgBank: [],
}

const getters = {
  accessOrgBank: state => state.accessOrgBank,
}

const actions = {
  async getAccessOrgBank({ commit, rootGetters }, { force = false } = {}) {
    if (!force && state.accessOrgBank.length) {
      return state.accessOrgBank
    }

    const {
      user: { queryAddressCode },
      clientId,
    } = rootGetters
    const params = {
      'agency_type.eq': 1,
      'bus_type.eq': 1,
      'orWhereAnd.is_county_access.eq': 0,
      'orWhereAnd.address_code.endlike': addressCut(queryAddressCode),
      'orWhereAnd.is_county_access.in': 1,
      'orWhereAnd.service_id.eq': clientId,
      // 'orWhere.is_county_access.eq': 0,
      // 'orWhere.service_id.eq': clientId,
    }

    const res = await getData('credit', 'credit_agency_access_bank_view_select', params, {
      agency_type_sub: 'asc',
    })

    const data = (res && res.data) || []
    commit('accessOrgBank', data)
    return data
  },
}

const mutations = {
  accessOrgBank: (state, payload) => (state.accessOrgBank = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
